package org.jeecg.modules.santu.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.santu.entity.TbCell;
import org.jeecg.modules.santu.service.ITbCellService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 格子
 * @Author: jeecg-boot
 * @Date:   2025-04-06
 * @Version: V1.0
 */
@Api(tags="格子")
@RestController
@RequestMapping("/santu/tbCell")
@Slf4j
public class TbCellController extends JeecgController<TbCell, ITbCellService> {
	@Autowired
	private ITbCellService tbCellService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tbCell
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "格子-分页列表查询")
	@ApiOperation(value="格子-分页列表查询", notes="格子-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TbCell tbCell,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TbCell> queryWrapper = QueryGenerator.initQueryWrapper(tbCell, req.getParameterMap());
		Page<TbCell> page = new Page<TbCell>(pageNo, pageSize);
		IPage<TbCell> pageList = tbCellService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/create")
	 public Result<?> create(@RequestParam(name="number", defaultValue="500") Integer number) {
		 System.out.println("number---->" + number);
		 this.createCell(number);
		 return Result.OK("创建成功！");
	 }

	 @GetMapping(value = "/open")
	 public Result<?> open(@RequestParam(name="code") String code,@RequestParam(name="codeStr") String codeStr) {
		 System.out.println("code--->" + code);
		 System.out.println("codeStr--->" + codeStr);
		 QueryWrapper<TbCell> tbCellQueryWrapper = new QueryWrapper<>();
		 tbCellQueryWrapper.lambda().eq(TbCell::getCellCode,code);
		 TbCell one = tbCellService.getOne(tbCellQueryWrapper);
		 System.out.println("one------>"+one);
		 if(one!=null){
		 	//判断是否已经开锁状态
			 Integer cellStatus = one.getCellStatus();
			 if(cellStatus==TbCell.ON){
			 	//已经是开锁状态
				 return Result.OK("已经是开锁状态！");
			 }else{
			 	//关锁状态
				 one.setCellStatus(TbCell.ON);
				 boolean b = tbCellService.updateById(one);
				 System.out.println("update----->" + b);
				 return Result.OK("开锁成功！");
			 }
		 }else{
			 return Result.error("未找到对应的柜门！");
		 }
	 }

	 @GetMapping(value = "/deleteCreate")
	 public Result<?> deleteCreate(@RequestParam(name="number", defaultValue="500") Integer number) {
		 //先删除所有
		 List<TbCell> list = tbCellService.list();
		 for (int i = 0; i < list.size(); i++) {
			 TbCell item = list.get(i);
			 tbCellService.removeById(item.getId());
		 }
		 //再创建
		 this.createCell(number);
		 return Result.OK("删除并创建成功！");
	 }

	 @GetMapping(value = "/deleteAll")
	 public Result<?> deleteAll() {
		 //先删除所有
		 List<TbCell> list = tbCellService.list();
		 for (int i = 0; i < list.size(); i++) {
			 TbCell item = list.get(i);
			 tbCellService.removeById(item.getId());
		 }
		 return Result.OK("删除所有柜子成功！");
	 }

	 private void createCell(int number) {
		 for (int i = 0; i < number; i++) {
			 TbCell tbCell = new TbCell();
			 tbCell.setCellNumber(i);
			 tbCell.setCellStatus(TbCell.CLOSE);
			 boolean save = tbCellService.save(tbCell);
			 System.out.println("save-------->"+i+":------>"+ save);
		 }
	 }

	/**
	 *   添加
	 *
	 * @param tbCell
	 * @return
	 */
	@AutoLog(value = "格子-添加")
	@ApiOperation(value="格子-添加", notes="格子-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TbCell tbCell) {
		tbCellService.save(tbCell);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tbCell
	 * @return
	 */
	@AutoLog(value = "格子-编辑")
	@ApiOperation(value="格子-编辑", notes="格子-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TbCell tbCell) {
		tbCellService.updateById(tbCell);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "格子-通过id删除")
	@ApiOperation(value="格子-通过id删除", notes="格子-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tbCellService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "格子-批量删除")
	@ApiOperation(value="格子-批量删除", notes="格子-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbCellService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "格子-通过id查询")
	@ApiOperation(value="格子-通过id查询", notes="格子-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		TbCell tbCell = tbCellService.getById(id);
		if(tbCell==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tbCell);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tbCell
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbCell tbCell) {
        return super.exportXls(request, tbCell, TbCell.class, "格子");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbCell.class);
    }

}
